package com.sls.game.parser.enfu;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sls.game.pojo.enfu.EnfuBalanceInfo;

/**
 * 
 * @description 恩付余额查询
 * @author Wangxuegang
 * @date 2013-5-3
 */
public class EnfuBalanceResponse {

	private static Logger log = Logger.getLogger(EnfuBalanceResponse.class);

	/**
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static EnfuBalanceInfo parseBalance(String xmlStr) {
		EnfuBalanceInfo info = new EnfuBalanceInfo();
		InputSource source = null;
		StringReader read = null;
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			read = new StringReader(xmlStr);
			source = new InputSource(read);
			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(source);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("apiresult".equals(node.getNodeName())) { // 第二层节点
					Element apiresultNode = (Element) node.getChildNodes();
					NodeList apiresultNodes = apiresultNode.getChildNodes();
					for (int j = 0; j < apiresultNodes.getLength(); j++) {
						Node grandsonNode = apiresultNodes.item(j);
						if ("resultcode".equals(grandsonNode.getNodeName())) {
							String resultcode = grandsonNode.getTextContent();
							info.setResultcode(resultcode);
						} else if ("resultdescription".equals(grandsonNode
								.getNodeName())) {
							String resultdescription = grandsonNode
									.getTextContent();
							info.setResultdescription(resultdescription);
						}
					}
				} else if ("merchantinfo".equals(node.getNodeName())) {
					Element merchantinfoNode = (Element) node.getChildNodes();
					NodeList merchantinfoNodes = merchantinfoNode
							.getChildNodes();
					for (int j = 0; j < merchantinfoNodes.getLength(); j++) {
						Node grandsonNode = merchantinfoNodes.item(j);
						if ("merchantid".equals(grandsonNode.getNodeName())) {
							String merchantid = grandsonNode.getTextContent();
							info.setMerchantid(merchantid);
						} else if ("accountbalance".equals(grandsonNode
								.getNodeName())) {
							String accountbalance = grandsonNode
									.getTextContent();
							info.setAccountbalance(accountbalance);
						}
					}
				}
			}
			return info;
		} catch (Exception e) {
			log.error(e);
		}

		return null;
	}

	// public static void main(String[] args){
	// StringBuilder sb = new StringBuilder();
	// sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
	// sb.append("<response>");
	// sb.append("<apiresult>");
	// sb.append("<resultcode>0</resultcode>");
	// sb.append("<resultdescription>查询成功</resultdescription>");
	// sb.append("</apiresult>");
	// sb.append("<merchantinfo>");
	// sb.append("<merchantid>1001</merchantid>");
	// sb.append("<accountbalance>999.99</accountbalance>");
	// sb.append("</merchantinfo>");
	// sb.append("</response>");
	//
	// String xmlStr = sb.toString().trim();
	// parseBalance(xmlStr);
	// }

}
